package com.lormann.android.infoterminal;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

public class infogatherservice extends Service {

	private NotificationManager _nm;
	private Timer localtimer;
	private final Random mGenerator= new Random();
	private final IBinder mBinder=new LocalBinder();
	private final Messenger mMessenger=new Messenger(new LocalMessengerHandler());
	
	static  final int MSG_SERVICE_EVENT_1=1;
	
	
	public class LocalMessengerHandler extends Handler{

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch(msg.what)
			{
			case MSG_SERVICE_EVENT_1: 
				Toast.makeText(getApplicationContext(), "Service Event is got and handled !!!!! ", Toast.LENGTH_SHORT).show();
				break;
			default:
				super.handleMessage(msg);
			}
			
		}
		
	} 
	
	public Messenger getMessengerHandle(){
		return mMessenger;
	}
	public class LocalBinder extends Binder{
		infogatherservice getService(){
			return infogatherservice.this;
		}
		
		IRemoteService.Stub getIRemoteService(){
			return mRemoteServiceBinder;
		};
	};

	//Generate a random integer between 1 to 100
	public int getRandomNumber(){
		
		return mGenerator.nextInt(100);
	}
	
	private final IRemoteService.Stub mRemoteServiceBinder=new IRemoteService.Stub(){

		@Override
		public int getPid() throws RemoteException {
			// TODO Auto-generated method stub
			return 0;//Process.myPid();
		}

		@Override
		public int getRandomNumber_AIDL() throws RemoteException {
			// TODO Auto-generated method stub
			return getRandomNumber();
		}
		
	};
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		
		Log.d(TAG,"onCreate !! ");
		Toast.makeText(infogatherservice.this, this.toString()+" : onCreate !! ",
				Toast.LENGTH_SHORT).show();
		
		
		//create and show notification 
		_nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
		Notification notification=new Notification(R.drawable.icon,"Info. gather Service Started !!!! ",System.currentTimeMillis());
		notification.flags=Notification.FLAG_AUTO_CANCEL;
		//Notification.FLAG_AUTO_CANCEL
		//put notification id in intent for cancel relevant notification
		Intent intent=new Intent(this,infoterminal.class);
		Bundle bundle=new Bundle(); 
		bundle.putInt("NOTIFICATION_KEY",1); //<---------------------------------------- only this value passed ??????
		intent.putExtras(bundle);
		
		PendingIntent p_intent=PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
	
		notification.setLatestEventInfo(this,"Info Gather Service ","The Service is created !! ",p_intent);
		
		_nm.notify(1,notification);
		
		
		localtimer=new Timer(true);
		
		
		super.onCreate();
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.d(TAG,"onDestroy !! ");
		Toast.makeText(infogatherservice.this, this.toString()+" : onDestroy !! ",
				Toast.LENGTH_SHORT).show();
		
		//create and show notification 
		Notification notification=new Notification(R.drawable.icon,"Info. gather Service Destroyed !!!! ",System.currentTimeMillis());
		notification.flags=Notification.FLAG_AUTO_CANCEL;
		//put notification id in intent for cancel relevant notification
		Intent intent=new Intent(this,infoterminal.class);
		Bundle bundle=new Bundle();
		bundle.putInt("NOTIFICATION_KEY",3);
		intent.putExtras(bundle);
		
		PendingIntent p_intent=PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
	
		notification.setLatestEventInfo(this,"Info Gather Service ","The Service is Destroyed !! ",p_intent);
		
		_nm.notify(3,notification);

		
		super.onDestroy();

	}
	@Override
	public void onRebind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onRebind !! ");
		Toast.makeText(infogatherservice.this, this.toString()+" : onRebind !! ",
				Toast.LENGTH_SHORT).show();
		super.onRebind(intent);
	}
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onStart !! ");
		Toast.makeText(infogatherservice.this, this.toString()+" : onStart !! ",
				Toast.LENGTH_SHORT).show();
		
		localtimer.schedule(new TimerTask(){
			public void run(){
				
		        Looper.prepare();
		       
		       
				// TODO Auto-generated method stub
				Log.d(TAG,"Timer expired  !! ");
				
				//Can't do this under timer expired run. lopper.prepare() is needed.!!!!!!!!
				Toast.makeText(infogatherservice.this, infogatherservice.this.toString()+" : Timer expired  !!  ",
					Toast.LENGTH_SHORT).show();
				
				//create and show notification 
				Notification notification=new Notification(R.drawable.icon,"Info. gather Service's timer is expired !!!! ",System.currentTimeMillis());
				notification.flags=Notification.FLAG_AUTO_CANCEL;
				//put notification id in intent for cancel relevant notification
				Intent intent=new Intent(infogatherservice.this,infoterminal.class);
				Bundle bundle=new Bundle();
				bundle.putInt("NOTIFICATION_KEY",2);
				intent.putExtras(bundle);
				
				PendingIntent p_intent=PendingIntent.getActivity(infogatherservice.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
				
				notification.setLatestEventInfo(infogatherservice.this,"Info Gather Service ","The Service's timer is expired !! ",p_intent);
				
				_nm.notify(2,notification);
				
				 Looper.loop();
			}
			
		},10000);
		
		super.onStart(intent, startId);
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onStartCommand !! ");
		Toast.makeText(infogatherservice.this, this.toString()+" : onStartCommand !! ",
				Toast.LENGTH_SHORT).show();
		return super.onStartCommand(intent, flags, startId);
	}
	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onUnbind !! ");
		Toast.makeText(infogatherservice.this, this.toString()+" : onUnbind !! ",
				Toast.LENGTH_SHORT).show();
		return super.onUnbind(intent);
	}
	private static final String TAG="infoGatherService ";
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onBind !! ");
		Toast.makeText(infogatherservice.this, this.toString()+" : onBind !! ",
				Toast.LENGTH_SHORT).show();
		
		return mBinder;
		
	}

}
